import {defineStore} from 'pinia'
import type {useLogin,Getinfo} from '@/api/useInfo'
import api from '@/api/useInfo'

interface useInfoStateData{
  token:string|null,
  userInfo:Getinfo
}
export const useUserInfoStore=defineStore('userInfo',{
  state():useInfoStateData{
    return{
      token:localStorage.getItem('token_key')||'',
      userInfo:{
        "routes":[],
        "buttons":[],
        "roles":[],
        "name":'',
        "avatar":''
      }
    }
  },
  actions:{
    //登录
    async login(useLogin:useLogin){
      try{
        const result=await api.useLogin(useLogin)
        this.token=result.token
        localStorage.setItem('token_key',result.token)
        return 'ok'
      }catch(error){
        return Promise.reject(error)
      }
    },
    async getUserinfo(){
      try {
        const result =await api.getInfo()
        this.userInfo=result
        return 'ok'
      } catch (error) {
        return Promise.reject(error)
      }
    },
    async reset(){
      this.token='',
      // this.userInfo={
      //   "routes":[],
      //   "buttons":[],
      //   "roles":[],
      //   "name":'',
      //   "avatar":''
      // }
      Object.assign(this.userInfo,{
        "routes":[],
        "buttons":[],
        "roles":[],
        "name":'',
        "avatar":''
      })
      localStorage.removeItem('token_key')
    },
    async logOut(){
      try {
        await api.OutLogin()
        this.reset()
        return 'ok'
      } catch (error) {
        return Promise.reject(error)
      }
    }
  },
  getters:{}
})